package cn.jslancer.webx.context;

import cn.jslancer.webx.request.ServerRequest;
import cn.jslancer.webx.response.ServerResponse;
import cn.jslancer.webx.rest.RestResponse;
import cn.jslancer.webx.router.RoutingContext;
import com.alibaba.fastjson.JSON;
import io.netty.handler.codec.http.HttpHeaderValues;

/**
 * @author zhuangchao
 * @date 2019/12/27 11:10
 */
public final class WebxContext implements RoutingContext {

    private ServerRequest request;

    private ServerResponse response;

    public WebxContext() {
    }

    public WebxContext(ServerRequest request, ServerResponse response) {
        this.request = request;
        this.response = response;
    }

    @Override
    public void json(RestResponse resp) {
        this.response.setContent(JSON.toJSONString(resp));
        this.response.setContentType(HttpHeaderValues.APPLICATION_JSON.toString());
        this.response.write();
    }

    @Override
    public void next() {

    }

    public void html() {

    }


    public void setValue(String key, Object value) {
        this.request.setAttribute(key, value);
    }

    public Object getValua(String key) {
        return this.request.getAttribute(key);
    }


    @Override
    public ServerRequest request() {
        return this.request;
    }

    @Override
    public ServerResponse response() {
        return this.response;
    }
}

